#include "zdfl_parameter_define.h"
#include "User_handle.h"
#include "app_OSD.h"
#include "zdfl_app_ctrl.h"
#include "zdfl_core_mwui.h"
#include "Printf.h"

#define MAX_FONT_SET    6

extern U8 getCanvasHardwareid(U8 cid);
    
/*
    zapp_pages: type: APP_Pages, max pages number is  MAX_APP_PAGEAS, you can change this to adjusted RAM;
                Function: use to remember the page's information have been opened;
*/
static APP_Pages zapp_pages;
/*
    zapp_reg_ctrls: type: APP_RegCtrls, max ctrl number is MAX_APP_REG_CTRLS, you can adjust it to adjust RAM;
                    Function: use to remember the ctrl's information that have been opened or one have been closed but need to remember state;
*/
static APP_RegCtrls zapp_reg_ctrls;
/*
    zsys_timer: type: UISYSTIMER, include maximum MAX_APP_TIMER timers; you can adjust it if have no enough requirement;
                Function: use to schedle the dynamic timer;
*/
static UISYSTIMER zsys_timer;
/*
    zsys_que:   type: msg_que_t, include maximum QUE_MSG_NUMBER message in queue; 
                Function: use to buffer the message;
*/
static msg_que_t zsys_que;
/*
    zsys_lock:  type: lock_data_t; 
                Function: use to sync operate message;
                access functions: zmsg_lock_init(), zmsg_lock(), zmsg_unlock();
*/
static lock_data_t zsys_lock;

/*
    zcore_pages_addr : type void **
                       Function: this was used to registers application pages array start address; all application pages' address was 
                       save to one const array; this array start address was send to zcore_pages_addr;
    zcore_pages_number: type U16
                       Function: save the numbers of application pages;    
    zcore_ctrls_addr : type void **
                       Function: this was used to registers application strls array start address; all application ctrls' address was 
                       save to one const array; this array start address was send to zcore_ctrls_addr;
    zcore_ctrls_number: type U16
                       Function: save the numbers of application ctrls;                       

*/
static msg_data_t MSG;    //erek for C51, 2009-12-20
static LPBASICWIN lpbasic;    //erek for C51, 2010-02-23
//static LPUIPBUTTON lpCtrl;

//static LPUIPAGE lpPage;

static void ** zcore_pages_addr;
static void ** zcore_ctrls_addr;
static U16 zcore_pages_number;
static U16 zcore_ctrls_number;

static U8 need_update_draw;

static lofi_t seq_sub_win;


static void zmsg_lock();
static void zmsg_unlock();
static U16 put_sys_que_message(msg_data_t * msg);
static void zinit_sys_queue(msg_que_t* pmq);
static U16 zput_sys_que_message(msg_data_t * msg);
void zUIDestroyOneSubPage(U8 pageid);
U8 LOFI_POP();

#ifdef Support_PagerSlide
extern U8 PagerSlideStatus;
extern U8 PagerSlideDirection;
#endif



static void memset(void *ptr, int value, int size)
{
    int i;
    char *p = ptr;
    for(i=0; i<size; i++)
        *(p+i) = (BYTE) value;
}


void zinit_core_timer(DWORD loop_counter)
{
    U8 ii;
    memset(&zsys_timer, 0, sizeof(UISYSTIMER));
    zsys_timer.loop_counter = (BYTE) loop_counter;
    for(ii=0;ii<MAX_APP_TIMER;ii++)
    {
        zsys_timer.timer[ii].onoff = CHAR_255;
    }
}

static void zinit_core_pages()
{
    int ii;
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        zapp_pages.APages[ii].status = (U8)PAGE_UNACTIVE;
    }
    zapp_pages.number = 0;
}

void ZSet_Draw_Update(U8 flag)
{
    need_update_draw = flag;
}

U8 ZGet_Draw_Update()
{
    return need_update_draw;
}

static void zinit_core_ctrls()
{
    int ii;
    memset(&zapp_reg_ctrls, 0, sizeof(APP_RegCtrls));
    zapp_reg_ctrls.number = 0;
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        zapp_reg_ctrls.ARegCtrls[ii].pageid = 255;
    }    
}

static void init_sub_win_seq()
{
    seq_sub_win.number = 0;
}

void ZGuiEnvInit()
{
    zinit_core_pages();
    zinit_core_ctrls();
    init_sub_win_seq();
    zinit_sys_queue(&zsys_que);
    ZSet_Draw_Update(0);
}

U8 ZCreateTimer(WORD ctrl_id, WORD interval, WORD start_val, U8 flag)
{
    U8 ii;
    for(ii=0;ii<MAX_APP_TIMER;ii++)
    {
        if(zsys_timer.timer[ii].onoff == CHAR_255)
        {
            break;
        }
    }
    if(ii >= MAX_APP_TIMER)
    {
        return 255;
    }
    zsys_timer.timer[ii].ctrl_id = ctrl_id;
    zsys_timer.timer[ii].timer_id = ii;
    zsys_timer.timer[ii].interval = interval;
    zsys_timer.timer[ii].time_elapse = start_val;
    zsys_timer.timer[ii].msg_counter = 0;
    if(flag == CHAR_0)
    {
        zsys_timer.timer[ii].onoff = 0;
    }
    else
    {
        zsys_timer.timer[ii].onoff = 1;
    }
    zsys_timer.number ++;
    return ii;
}

U8 ZKillTimer(WORD hCtrl)
{
    U8 ii;
    for(ii=0;ii<MAX_APP_TIMER;ii++)
    {
        if(zsys_timer.timer[ii].ctrl_id == hCtrl)
        {
            zsys_timer.timer[ii].onoff = CHAR_255;
        }
    }
}

U8 ZStartTimer(U8 timer_id)
{
    zsys_timer.timer[timer_id].onoff = (U8)1;
}

U8 ZStopTimer(U8 timer_id)
{
    zsys_timer.timer[timer_id].onoff = (U8)0;
}

U8 ZSendTimerMsg(U8 timer_id)
{
    zsys_timer.timer[timer_id].msg_counter ++;
    ZSendMesssage(CMD_TIMER, timer_id, zsys_timer.timer[timer_id].ctrl_id, zsys_timer.timer[timer_id].msg_counter, 0);
}
U8 ZGetSystemTimeLooper(void)
{
    return zsys_timer.loop_counter;
}

U8 ZUITimerRun(void)

{
    int ii;
    for(ii=0;ii<MAX_APP_TIMER;ii++)
    {
        if(zsys_timer.timer[ii].onoff == (U8)1)
        {
            zsys_timer.timer[ii].time_elapse ++;
            if(zsys_timer.timer[ii].time_elapse >= zsys_timer.timer[ii].interval)
            {
                zsys_timer.timer[ii].time_elapse = 0;
                ZSendTimerMsg(ii);
            }
        }
    }
}



char * ZMemCpy(void * des1, void * src1, int counter)
{
#if 0
    memcpy(des, src, counter);
#else
    char *des = des1;
    char *src = src1;

    int ii;
    for(ii=0;ii<counter;ii++)
    {
        *des++ = *src++;
    }
#endif
}

void ZUIDestroyActivePage()
{
    LPUIPAGE lppage;
    if(zapp_reg_ctrls.number<=0)return;
    lppage = zcore_pages_addr[zapp_pages.APages[zapp_pages.top_index].page_index];
    TWLL_Canvas_OnOff(lppage->nWinno,0);
    zUIDestroyPage(zapp_pages.top_index);
}


void zUIDestroyPage(U8 pageid)
{
    static int ii;
    static U8 PagerID;//erek for C51, 2010-02-23
    //msg_data_t MSG;

	PagerID=pageid;
	
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].pageid == zapp_pages.APages[PagerID].page_index)
            {
                MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
       	  MSG.mtype=MWUI_PUBLIC;
       	  MSG.mparam=MWUI_DESTROY;
       	  MSG.mparal=0;
       	  MSG.mparan=0;
		  zapp_reg_ctrls.ARegCtrls[ii].CallBack(&MSG);
                zapp_reg_ctrls.ARegCtrls[ii].pageid = (U8)255;
                zapp_reg_ctrls.ARegCtrls[ii].ctrl_id = 65535;
                zapp_reg_ctrls.number --;
            }
        }
    }
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].page_index == zapp_pages.APages[PagerID].page_index && zapp_pages.APages[ii].status == PAGE_ACTIVE)
        {
            int jj;
            zapp_pages.APages[ii].status = PAGE_UNACTIVE;
            zapp_pages.number --;
            zapp_pages.top_index = 0;
            zUIRedrawPages(0);
            if(zapp_pages.number>1)
            {
                for(ii=1;ii<MAX_APP_PAGEAS;ii++)
                {
                    if(zapp_pages.APages[ii].status == PAGE_ACTIVE)
                        zapp_pages.top_index = ii;
                }
                zUIRedrawPages(zapp_pages.top_index);
            }                        
            return;
        }
    }
}


void zUIActiveParentSubPage(U8 pageid)
{
    static int ii;
    static U8 PagerID;//erek for C51, 2010-02-23
    //msg_data_t MSG;

    PagerID=pageid;
	
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].pageid == zapp_pages.APages[PagerID].page_index)
            {
                MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
       	  MSG.mtype=MWUI_PUBLIC;
       	  MSG.mparam=MWUI_DESTROY;
       	  MSG.mparal=0;
       	  MSG.mparan=0;
                zapp_reg_ctrls.ARegCtrls[ii].CallBack(&MSG);
                zapp_reg_ctrls.ARegCtrls[ii].pageid = (U8)255;
                zapp_reg_ctrls.ARegCtrls[ii].ctrl_id = 65535;
                zapp_reg_ctrls.number --;
            }
        }
    }
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].page_index == zapp_pages.APages[PagerID].page_index && zapp_pages.APages[ii].status == PAGE_ACTIVE)
        {
            zapp_pages.APages[ii].status = PAGE_UNACTIVE;
            zapp_pages.number --;
            zapp_pages.top_index = 0;
            return;
        }
    }
}


void ZActiveSeqSubPage()
{
    WORD page_id, active_ctrl_id;
    if(seq_sub_win.number<2)return;
    page_id = seq_sub_win.subWin[seq_sub_win.number-1].page_id;
    zapp_pages.top_index = page_id;
}

BYTE ZGetCSubPageCanvasID()
{
    BYTE cid;
    cid = seq_sub_win.subWin[seq_sub_win.number-1].canvas_id;
    return cid;
}

void ZUIDestroyActiveSubPage()
{
    U8 pid;
    pid = LOFI_POP();
    zUIDestroyOneSubPage(pid);
}

void zUIDestroyOneSubPage(U8 pageid)
{
    static int ii;
    static U8 PagerID;//erek for C51, 2010-02-23
    //msg_data_t MSG;

    PagerID=pageid;
	
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].pageid == zapp_pages.APages[PagerID].page_index)
            {
                //MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
       	  //MSG.mtype=MWUI_PUBLIC;
       	  //MSG.mparam=MWUI_DESTROY;
       	  //MSG.mparal=0;
       	  //MSG.mparan=0;
                //zapp_reg_ctrls.ARegCtrls[ii].CallBack(&MSG);
                zapp_reg_ctrls.ARegCtrls[ii].pageid = (U8)255;
                zapp_reg_ctrls.ARegCtrls[ii].ctrl_id = 65535;
                zapp_reg_ctrls.number --;
            }
        }
    }
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].page_index == zapp_pages.APages[PagerID].page_index && zapp_pages.APages[ii].status == PAGE_ACTIVE)
        {
            zapp_pages.APages[ii].status = PAGE_UNACTIVE;
            zapp_pages.number --;
            zapp_pages.top_index = 0;
            return;
        }
    }
}

void LOFI_PUSH(U8 cid, WORD pid)
{
    if(seq_sub_win.number<MAX_SUB_WIN_NO)
    {
        seq_sub_win.subWin[seq_sub_win.number].canvas_id = cid;
        seq_sub_win.subWin[seq_sub_win.number].page_id = pid;
        seq_sub_win.number ++;
    }
}

void LOFI_SET_MAIN_PAGE(U8 cid, WORD pid)
{
    seq_sub_win.subWin[0].canvas_id = cid;
    seq_sub_win.subWin[0].page_id = pid;
    seq_sub_win.number = 1;
}

U8 LOFI_POP()
{
    lofi_unit_t ud;
    if(seq_sub_win.number>0)
    {
        ud.page_id = seq_sub_win.subWin[seq_sub_win.number-1].page_id;
        seq_sub_win.number --;
    }
    else
    {
        ud.page_id = 0;
    }
    return ud.page_id;
}


lofi_unit_t LOFI_Get_Data(int id)
{
    lofi_unit_t ud;
    int index;
    if(id == -1 && seq_sub_win.number>1)
    {
        index = seq_sub_win.number-2;
    }
    else
    {
        index = id;
    }
    if(id<seq_sub_win.number && id>=0)
    {
        ud.canvas_id = seq_sub_win.subWin[index].canvas_id;
        ud.page_id = seq_sub_win.subWin[index].page_id;
    }
    else
    {
        ud.canvas_id = 0;
        ud.page_id = 0;
    }
    return ud;
}

U8 isValidLofiCanvasID(U8 cid)
{
    int ii;
    U8 hid,h1id;
    hid = getCanvasHardwareid(cid);
    for(ii=0;ii<seq_sub_win.number;ii++)
    {
        h1id = getCanvasHardwareid(seq_sub_win.subWin[ii].canvas_id);
        if(h1id == hid)
        {
            break;
        }
    }
    if(ii<seq_sub_win.number)
    {
        return 255;
    }
    else
    {
        return cid;
    }
}


#ifdef Used_BlockTransfer
  void UIDesTroyAllActivePage(U8 CanType)
#else
void UIDesTroyAllActivePage()
#endif
{
    int ii;
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status != PAGE_UNACTIVE)
        {
            LPUIPAGE lppage;
            if(zapp_pages.APages[ii].page_index < zcore_pages_number)
            {
                lppage = zcore_pages_addr[zapp_pages.APages[ii].page_index];
		  #ifndef Used_BlockTransfer
                TWLL_Canvas_OnOff(lppage->nWinno,0);
		  #else
		  if((User_Canvas[lppage->nWinno].Bits_Per_Pixel==8)||(CanType==8))
		  TWLL_Canvas_OnOff(lppage->nWinno,0);
		  #endif
                zUIDestroyOneSubPage(ii);
            }
        }
    }
    seq_sub_win.number = 0;
}

U8 ZSetCtrlFocus(WORD ctrl_id, U8 cmd)
{
    //LPUIPBUTTON lpCtrl;
    U8 retval = 255;
    static U8 CMD;
    static U16 Ctrl_ID; //erek for C51, 2010-02-23
	LPUIPBUTTON lpbtn;

	Ctrl_ID = ctrl_id;
	CMD = cmd;
	
    if (Ctrl_ID > zcore_ctrls_number)
    {
        return retval;
    }
	
    lpbasic = (LPBASICWIN)zcore_ctrls_addr[Ctrl_ID];
    if (lpbasic == 0)
    {
        return retval;
    }
	
    if ((U8)lpbasic->type[0] != (U8)WGUI_BUTTON && (U8)lpbasic->type[0] != (U8)WGUI_SCROLLBAR)
    {
        return retval;
    }
	
    /*
    if((U8)(lpbasic->dFlags&UI_FLAG_DISABLE) != (U8)0)
    {
        return retval;
    }
    */

	if (lpbasic->type[0] == (U8)WGUI_SCROLLBAR)
    {
        U8 sta;
        ZGetCtrlParam(Ctrl_ID, ZVAL_VALUE, &sta);
        if ((U8)0 == (U8)CMD)
        {
            sta = sta|0x80;
        }
        else
        {
            sta = sta&0x7f;
        }
        ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, sta);
        retval = 0;

		DrawScrollbarControl((LPUISCROLLBAR)lpbasic);
    }
    else
    {
		MSG.mitem  = Ctrl_ID;    //erek for C51, 2009-12-20
		MSG.mtype  = MWUI_PUBLIC;
		MSG.mparam = MWUI_CLEAR;
		MSG.mparal = 0;
		MSG.mparan = 0;
		lpbasic->CallBack(&MSG);
        /*if(CMD)
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 0);
        }
        else
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 1);
        }*/
		if (CMD == 1) //2010-12-6 erek for realizing the button three status 
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 0);
        }
        else if (CMD == 0)
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 1);
        }
		else if (CMD == 2)
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 2);
        }
		else if (CMD == 3)
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 3);
        }
        retval = 0;

		lpbtn = (LPUIPBUTTON)lpbasic;
    	if ((lpbtn->hImageID[0] == (U16)65535) && (lpbtn->textID[0] == (int)65535))
    	{
    		;
    	}
    	else
    	{
    		//Printf("\nlpbtn->hImageID[0]=%x,lpbtn->textID[0] =%x\r\n",lpbtn->hImageID[0],lpbtn->textID[0] );
        	DrawButtonControl((LPUIPBUTTON)lpbasic);
    	}

	//DrawButtonControl((LPUIPBUTTON)lpbasic);
    }
    //lpCtrl->OnDraw(lpCtrl);
    return 0;
}

#if 0
U8 zSetCtrlFocusA(WORD ctrl_id, U8 cmd)
{
    //LPUIPBUTTON lpCtrl;
    U8 retval = 255;
    static U8 CMD;
    static U16 Ctrl_ID; //erek for C51, 2010-02-23
    LPUIPBUTTON lpbtn;

	Ctrl_ID=ctrl_id;
	CMD=cmd;
	
    if(Ctrl_ID>zcore_ctrls_number)
    {
        return retval;
    }
    lpbasic = zcore_ctrls_addr[Ctrl_ID];
    if(lpbasic  == 0)
    {
        return retval;
    }
    if((U8)lpbasic->type[0] != (U8)WGUI_BUTTON/* && (U8)lpCtrl->type[0] != (U8)WGUI_SCROLLBAR*/)
    {
        return retval;
    }
    /*
    if((U8)(lpbasic->dFlags&UI_FLAG_DISABLE) != (U8)0)
    {
        return retval;
    }
    */
	
   MSG.mitem=Ctrl_ID;    //erek for C51, 2009-12-20
   MSG.mtype=MWUI_PUBLIC;
   MSG.mparam=MWUI_CLEAR;
   MSG.mparal=0;
   MSG.mparan=0;
   lpbasic->CallBack(&MSG);
        if(CMD)
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 0);
        }
        else
        {
            ZSetCtrlParam(Ctrl_ID, ZVAL_VALUE, 1);
        }
        retval = 0;
    //lpCtrl->OnDraw(lpCtrl);
     lpbtn = (LPUIPBUTTON)lpbasic;
    if(lpbtn->hImageID[0] == 65535 && lpbtn->textID[0] == 65535)
    {
    	;
    }
    else
    {
        DrawButtonControl((LPUIPBUTTON)lpbasic);
    }

    //DrawButtonControl((LPUIPBUTTON)lpbasic);
	
    return retval;
    
}
#endif

void zUIRedrawPages(int pageid)
{
    static int ii, aindex=-1;
    LPBASICWIN lpbasic;

	//Printf("\zUIRedrawPages++jj:%x\r\n");
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid == zapp_pages.APages[pageid].page_index)
        {
            lpbasic = zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
            //lpbasic->OnDraw(lpbasic); //erek for C51, 2010-02-23
            if(lpbasic->type[0] == WGUI_IMAGE)
        	DrawImageControl((LPUIIMAGE)lpbasic);
    	     else if(lpbasic->type[0] == WGUI_BUTTON)
        	DrawButtonControl((LPUIPBUTTON)lpbasic);
    	     else if(lpbasic->type[0] == WGUI_TEXT)
        	DrawTextControl((LPUITEXT)lpbasic);
    	     else if(lpbasic->type[0] == WGUI_PROGRESS)
        	DrawProgressControl((LPUIPROGRESS)lpbasic);
    	     else if(lpbasic->type[0] == WGUI_SCROLLBAR)
        	DrawScrollbarControl((LPUISCROLLBAR)lpbasic);
	 
            if(zapp_pages.APages[pageid].active_crtl_index == zapp_reg_ctrls.ARegCtrls[ii].ctrl_id)
            {
                aindex = ii;
            }
           }
       }
    if(aindex>=0)
    {
        ZSetCtrlFocus(zapp_reg_ctrls.ARegCtrls[aindex].ctrl_id, 1);
    }
}

U16 zget_valid_active_ctrl_index(U8 pageid, WORD ctrlid)
{
    int ii = 65535;
    
    if(pageid == 0)
    {
        for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255 && zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == ctrlid)
            {
                return ii;
            }
        }
    }
    
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid == (U8)255)
        {
            break;
        }
    }
    return ii;
}


U16 UICreateCtrl(U16 pageid, U16 indexid, U16 active_page_index, U8 value, UICALLBACK callback)
{
    U16 index;
    index = zget_valid_active_ctrl_index(pageid, indexid);
    if(index == 65535)
    {
        return 65535;
    }
    zapp_reg_ctrls.ARegCtrls[index].pageid = pageid;
    zapp_reg_ctrls.ARegCtrls[index].ctrl_id = indexid;
    zapp_reg_ctrls.ARegCtrls[index].value = value;
    zapp_reg_ctrls.ARegCtrls[index].CallBack = callback;
    zapp_reg_ctrls.number ++;
    return index;
}

U16 ZCreateCtrl(U16 ctrl_id)
{
    int ii;
    LPBASICWIN pbwin;
    
    if(ctrl_id >= zcore_ctrls_number)
    {    
        return 65535;
    }     
    pbwin = zcore_ctrls_addr[ctrl_id];
    if(pbwin == 0)
    {
        return 65535;
    }
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status = PAGE_ACTIVE && pbwin->hParent == zapp_pages.APages[ii].page_index)
        {
            return UICreateCtrl(pbwin->hParent, ctrl_id, ii, pbwin->user_value, pbwin->CallBack);
        }
    }
    return 65535;
}

void ZDestroyCtrl(U16 ctrl_id)
{
    static int ii;
    //LPBASICWIN pwin;
    static int type, Ctrl_ID;
    //msg_data_t MSG;

	Ctrl_ID=ctrl_id;
	
    if(Ctrl_ID>=zcore_ctrls_number)return;
    lpbasic = zcore_ctrls_addr[Ctrl_ID];
    type = lpbasic -> type[0];
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == Ctrl_ID)
        {
            zapp_reg_ctrls.ARegCtrls[ii].pageid = (U8)255;
            zapp_reg_ctrls.ARegCtrls[ii].ctrl_id = 65535;
	     MSG.mitem=Ctrl_ID;    //erek for C51, 2009-12-20
	     MSG.mtype=MWUI_PUBLIC;
	     MSG.mparam=MWUI_DESTROY;
	     MSG.mparal=0;
	     MSG.mparan=0;
            switch(type)
            {
                case WGUI_BUTTON:
                {
                    LPUIPBUTTON lpbtn = (LPUIPBUTTON)lpbasic;
                    if(lpbtn->CallBack)
                    {
                        lpbtn->CallBack(&MSG);
                    }
                }
                break;
                case WGUI_TEXT:
                {
                    LPUITEXT lptext = (LPUITEXT)lpbasic;
                    if(lptext->CallBack)
                    {
                        lptext->CallBack(&MSG);
                    }
                }
                break;
                case WGUI_IMAGE:
                {
                    LPUIIMAGE lpImage = (LPUIIMAGE)lpbasic;
                    if(lpImage->CallBack)
                    {
                        lpImage->CallBack(&MSG);
                    }
                }
                break;
                case WGUI_SCROLLBAR:
                {
                    LPUISCROLLBAR lpScr = (LPUISCROLLBAR)lpbasic;
                    if(lpScr->CallBack)
                    {
                        lpScr->CallBack(&MSG);
                    }
                }
                break;
                case WGUI_PROGRESS:
                {
                    LPUIPROGRESS lpPro = (LPUIPROGRESS)lpbasic;
                    if(lpPro->CallBack)
                    {
                        lpPro->CallBack(&MSG);
                    }
                }
                break;
                case WGUI_ANIMATER:
                {
                    LPUIANIMATOR lpAni = (LPUIANIMATOR)lpbasic;
                    if(lpAni->CallBack)
                    {
                        lpAni->CallBack(&MSG);
                    }
                }
                break;
            }
            return;
        }
    }
}


U8 zgetCtrlIDIndex(U8 ctrl_id)
{
    U8 index = 255;
    int ii;
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == ctrl_id && zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
        {
            index = ii;
            break;
        }
    }
    return index;    
}


U8 UICreateOnePage(U8 pageid, LPUIPAGE phandle)
{
    static int ii, jj, aindex=-1, rindex=0;
    static U16 aretIndex,Active_Control_ID,Start_CID,End_CID,PWinCtrlID;
    static LPUIPAGE lpPage;
    static U8 PagerID,NWINNO,NPalette_ID,cnt=0;

    //erek for C51, 2010-01-20
    PagerID=pageid;   //because the indirect callback funciton will destroy the space of local variable,
    NWINNO=phandle->nWinno;//because the indirect callback funciton will destroy the space of funciton parameters,
    Active_Control_ID=phandle->active_ctrl_id;
    NPalette_ID=phandle->nPalette_id;
    Start_CID=phandle->start_cid;
    End_CID=phandle->end_cid;

	//#ifdef Used_BlockTransfer
	//BlockTransStatus=1;
	//#endif

    if(phandle == 0)
    {
        return 255;
    }
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status == (U8)PAGE_UNACTIVE)
        {
            break;
        }
    }   
    if(ii>=MAX_APP_PAGEAS)
    {
        ii = MAX_APP_PAGEAS-1;
        zUIDestroyPage(ii);
    }
    else
    {
        LPBASICWIN pwin;
        zapp_pages.APages[ii].page_index = PagerID;
        zapp_pages.APages[ii].active_crtl_index = 65535;
        zapp_pages.APages[ii].display_layer = NWINNO;
        zapp_pages.APages[ii].status = PAGE_ACTIVE;
        zapp_pages.APages[ii].bkcolor = 1;
//		Printf("\nphandle->nWinno:%bd\r\n",NWINNO);
//		Printf("\nphandle->nPalette_id:%x\r\n",phandle->nPalette_id);
//		Printf("\nzapp_pages.APages[ii].active_crtl_index:%x\r\n",zapp_pages.APages[ii].active_crtl_index);
//		Printf("\nzapp_pages.APages[ii].status:%bd\r\n",zapp_pages.APages[ii].status);
        if(phandle->subPage == 0)
        {
            LOFI_SET_MAIN_PAGE(NWINNO, ii);
        }
        else
        {		    
            LOFI_PUSH(NWINNO, ii);		    
        }
        if(PagerID<zcore_pages_number)
        {
            lpPage = zcore_pages_addr[PagerID];
            if(lpPage->callback)
            {
				MSG.mitem=PagerID;    //erek for C51, 2009-12-20
				MSG.mtype=MWUI_PUBLIC;
				MSG.mparam=MWUI_CREATE;
				MSG.mparal=0;
				MSG.mparan=0;
				lpPage->callback(&MSG);

				MSG.mitem=PagerID;    //erek for C51, 2009-12-20
				MSG.mtype=MWUI_PUBLIC;
				MSG.mparam=MWUI_INIT;
				MSG.mparal=0;
				MSG.mparan=0;
				lpPage->callback(&MSG);
            }
        }
        zapp_pages.top_index = ii;
        for (jj=Start_CID; jj<End_CID; jj++)
        {
//			Printf("\nzcore_ctrls_number:%x\r\n",zcore_ctrls_number);
//			Printf("\njj:%x\r\n",jj);
//			Printf("\npageid:%bx\r\n",PagerID);
			pwin = zcore_ctrls_addr[jj];
			PWinCtrlID=pwin->CtrlID;
            if (pwin)
            {
//           	Printf("\npwin->hParent:%x\r\n",pwin->hParent);
                if ((U16)(pwin->hParent) == (U16)PagerID)
                {
                    cnt ++;
                    rindex ++;
                    aretIndex = UICreateCtrl(PagerID, jj, ii, pwin->user_value, pwin->CallBack);
                    if (pwin->CallBack)
                    {
						MSG.mitem=pwin->CtrlID;    //erek for C51, 2009-12-20
						MSG.mtype=MWUI_PUBLIC;
						MSG.mparam=MWUI_CREATE;
						MSG.mparal=0;
						MSG.mparan=0;
//						Printf("\nMSG->mitem0:%x 0x%lx\r\n",MSG.mitem, pwin->CallBack);	
//						Printf("\nMSG->mparam0:%x\r\n",MSG.mparam);
						pwin->CallBack(&MSG);
//						Printf("\nMSG->mitem:%x\r\n",MSG.mitem);	
//						Printf("\nMSG->mparam:%x\r\n",MSG.mparam);	
//						Printf("\npwin->CallBack:");
                    }
                    if(aretIndex != 65535 && PWinCtrlID == Active_Control_ID)
                    {
                        zapp_pages.APages[ii].active_crtl_index = Active_Control_ID;
                        zapp_pages.APages[ii].reg_ctrl_index = jj;
                        aindex = jj;
                    }
                }
            }
//			Printf("\n++jj:%x\r\n", jj);
//			Printf("\n++pageid:%bx\r\n", pageid);
        }
		//#ifdef Used_BlockTransfer
		//BlockTransStatus=0;
		//#endif
        zapp_pages.top_index = ii;
        zapp_pages.number ++;	
        if (aindex != -1)
        {
            LPBASICWIN lpCtrl;
            ZSetCtrlFocus(Active_Control_ID, 1);		
            ZApp_Set_Pallette(NWINNO, NPalette_ID);

            return ii;
        }	
        ZApp_Set_Pallette(NWINNO, NPalette_ID);

        return (U8)ii;
    }

	return 0;
}

//erek 2010-4-27 for pager slide function
#ifdef Support_PagerSlide
void ZDrawSlidePageCtrl(U8 pageid, U8 Direction)
{
    static LPUIPAGE phandle;
    static U16 jj,Start_CID,End_CID;
    static U8 PagerID;
    LPBASICWIN pwin;

    //erek for C51, 2010-01-20
    phandle=(LPUIPAGE)GetPageHandle(pageid);
    PagerID=pageid;   
    Start_CID=phandle->start_cid;
    End_CID=phandle->end_cid;

	PagerSlideStatus=1;
	PagerSlideDirection=Direction;
	//Printf("\n++Start_CID:%x\r\n",Start_CID);
		//Printf("\n++End_CID:%x\r\n",End_CID);
        
       for(jj=Start_CID;jj<End_CID;jj++)
       {
            pwin = zcore_ctrls_addr[jj];
            if(pwin)
            {
                if((U16)(pwin->hParent) == (U16)PagerID)
                {
                   UICreateCtrl(PagerID, jj, 0, pwin->user_value, pwin->CallBack);
                    if(pwin->CallBack)
                    {
                    	   MSG.mitem=pwin->CtrlID;    //erek for C51, 2009-12-20
                    	   //Printf("\n++pwin->CtrlID:%x\r\n",pwin->CtrlID);
	 		   MSG.mtype=MWUI_PUBLIC;
	 		   MSG.mparam=MWUI_CREATE;
	 		   MSG.mparal=0;
	 		   MSG.mparan=0;
                        pwin->CallBack(&MSG);
                    }
                    
                }
            }
			//Printf("\n++jj:%x\r\n",jj);
			
        }
	   
		//Printf("\n++pageid:%bx\r\n",PagerID);
	
	PagerSlideStatus=0;

}


void ZDestroySlidePage()
{
    int ii;
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status != PAGE_UNACTIVE)
        {
            if(zapp_pages.APages[ii].page_index < zcore_pages_number)
            {
                zUIDestroyOneSubPage(ii);
            }
        }
    }
    seq_sub_win.number = 0;
}

void ZActiveSlidePage(U8 pageid)
{
    int ii;
    LPUIPAGE phandle;
	
    phandle=(LPUIPAGE)GetPageHandle(pageid);
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status == (U8)PAGE_UNACTIVE)
        {
            break;
        }
    }  
    if(ii>=MAX_APP_PAGEAS)
    {
        ii = MAX_APP_PAGEAS-1;
        zUIDestroyPage(ii);
    }
    else
    {
        zapp_pages.APages[ii].page_index = pageid;
        zapp_pages.APages[ii].active_crtl_index = 65535;
        zapp_pages.APages[ii].display_layer = phandle->nWinno;
        zapp_pages.APages[ii].status = PAGE_ACTIVE;
        zapp_pages.APages[ii].bkcolor = 1;
		
        LOFI_SET_MAIN_PAGE(phandle->nWinno, ii);
       
        zapp_pages.top_index = ii;
        zapp_pages.number ++;	
    }
}
#endif

/*U8 UICreateOnePage(U16 pageid, LPUIPAGE phandle)
{
    int ii, jj, aindex=-1, rindex=0;
    U16 aretIndex,PagerID,Active_Control_ID,PWinCtrlID;
    LPUIPAGE lpPage;
    U8 NWINNO,NPalette_ID,cnt=0;

    PagerID=pageid;   //because the indirect callback funciton will destroy the space of local variable,
    NWINNO=phandle->nWinno;//because the indirect callback funciton will destroy the space of funciton parameters,
    Active_Control_ID=phandle->active_ctrl_id;
    NPalette_ID=phandle->nPalette_id;
   
    if(phandle == 0)
    {
        return 255;
    }
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status == (U8)PAGE_UNACTIVE)
        {
            break;
        }
    }   
    if(ii>=MAX_APP_PAGEAS)
    {
        ii = MAX_APP_PAGEAS-1;
        zUIDestroyPage(ii);
    }
    else
    {
        LPBASICWIN pwin;
        zapp_pages.APages[ii].page_index = PagerID;
        zapp_pages.APages[ii].active_crtl_index = 65535;
        zapp_pages.APages[ii].display_layer = NWINNO;
        zapp_pages.APages[ii].status = PAGE_ACTIVE;
        zapp_pages.APages[ii].bkcolor = 1;
		Printf("\nphandle->nWinno:%bd\r\n",phandle->nWinno);
		//Printf("\nphandle->nPalette_id:%x\r\n",phandle->nPalette_id);
		 //Printf("\nzapp_pages.APages[ii].active_crtl_index:%x\r\n",zapp_pages.APages[ii].active_crtl_index);
		// Printf("\nzapp_pages.APages[ii].status:%bd\r\n",zapp_pages.APages[ii].status);
        if(phandle->subPage == 0)
        {
            LOFI_SET_MAIN_PAGE(NWINNO, ii);
        }
        else
        {		    
            LOFI_PUSH(NWINNO, ii);		    
        }
        if(PagerID<zcore_pages_number)
        {
            lpPage = zcore_pages_addr[PagerID];
            if(lpPage->callback)
            {
            	  MSG.mitem=PagerID;    //erek for C51, 2009-12-20
	 	  MSG.mtype=MWUI_PUBLIC;
	 	  MSG.mparam=MWUI_CREATE;
	 	  MSG.mparal=0;
	 	  MSG.mparan=0;
                lpPage->callback(&MSG);

		  MSG.mitem=PagerID;    //erek for C51, 2009-12-20
	 	  MSG.mtype=MWUI_PUBLIC;
	 	  MSG.mparam=MWUI_INIT;
	 	  MSG.mparal=0;
	 	  MSG.mparan=0;
                lpPage->callback(&MSG);
            }
        }
        zapp_pages.top_index = ii;
        for(jj=0;jj<zcore_ctrls_number;jj++)
        {
        	//Printf("\nzcore_ctrls_number:%x\r\n",zcore_ctrls_number);
		//Printf("\njj:%x\r\n",jj);
		//Printf("\npageid:%x\r\n",PagerID);
            pwin = zcore_ctrls_addr[jj];
	     PWinCtrlID=pwin->CtrlID;
            if(pwin)
            {
            //Printf("\npwin->hParent:%x\r\n",pwin->hParent);
                if((U16)(pwin->hParent) == (U16)PagerID)
                {
                    cnt ++;
                    rindex ++;
                    aretIndex = UICreateCtrl(PagerID, jj, ii, pwin->user_value, pwin->CallBack);
                    if(pwin->CallBack)
                    {
                    	   MSG.mitem=pwin->CtrlID;    //erek for C51, 2009-12-20
	 		   MSG.mtype=MWUI_PUBLIC;
	 		   MSG.mparam=MWUI_CREATE;
	 		   MSG.mparal=0;
	 		   MSG.mparan=0;
			   //Printf("\nMSG->mitem0:%x\r\n",MSG.mitem);	
			//Printf("\nMSG->mparam0:%x\r\n",MSG.mparam);
                        pwin->CallBack(&MSG);
			//Printf("\nMSG->mitem:%x\r\n",MSG.mitem);	
			//Printf("\nMSG->mparam:%x\r\n",MSG.mparam);	
			 //Printf("\npwin->CallBack:");
                    }
                    if(aretIndex != 65535 && PWinCtrlID == Active_Control_ID)
                    {
                        zapp_pages.APages[ii].active_crtl_index = Active_Control_ID;
                        zapp_pages.APages[ii].reg_ctrl_index = jj;
                        aindex = jj;
                    }
                }
            }
			//Printf("\n++jj:%x\r\n",jj);
			//Printf("\n++pageid:%x\r\n",pageid);
        }
		//Printf("\n++phandle->nWinno:%x\r\n",phandle->nWinno);
		//Printf("\n++phandle->nPalette_id:%x\r\n",phandle->nPalette_id);
        zapp_pages.top_index = ii;
        zapp_pages.number ++;	
        if(aindex != -1)
        {
            LPBASICWIN lpCtrl;
            ZSetCtrlFocus(Active_Control_ID, 1);		
            ZApp_Set_Pallette(NWINNO, NPalette_ID);
            return ii;
        }	
        ZApp_Set_Pallette(NWINNO, NPalette_ID);
        return (U8)ii;
    }
	return 0;
}*/


int ZIsPointInRect(int px, int py, LPUIRECT prect)
{
    if(prect->left<=px && (px<=prect->left+prect->width) && prect->top<=py && py<=(prect->top+prect->height))
    {
        return 0;
    }
    return -1;
}

void ZSendMesssage(U8 msg_type, U16 msg_item, U16 param, U16 paral, U16 paran)
{
    //msg_data_t MSG;
    MSG.mtype = msg_type;
    MSG.mitem = msg_item;
    MSG.mparam = param;
    MSG.mparal = paral;
    MSG.mparan = paran;
    
    zput_sys_que_message(&MSG);
}

void zmsg_process_by_desktop(U8 uMsg, WORD param1, WORD param2, WORD param3 )
{
    //msg_data_t MSG;
    LPUIPAGE lpPage = zcore_pages_addr[0];
	
    if(lpPage->callback != 0)
    {
    	 MSG.mitem=0;    //erek for C51, 2009-12-20
	 MSG.mtype=uMsg;
	 MSG.mparam=param1;
	 MSG.mparal=param2;
	 MSG.mparan=param3;
        lpPage->callback(&MSG);
    }
}
#if 0
static U8 zmsg_touch_process_by_page(U16 px, U16 py, U16 type, U16 pageid)
{
    LPUIRECT lpRect;
    LPUIPAGE lpPage;
    UIRECT rect;
    ZPoint pt;
    //msg_data_t MSG;
	
    if(pageid > MAX_APP_PAGEAS-1)
    {
        return 255;
    }
    lpPage = (LPUIPAGE)zcore_pages_addr[zapp_pages.APages[pageid].page_index];
    lpRect = &lpPage->rect;
    zGetPageStartPoint(&pt, lpPage);
    if(ZIsPointInRect(px, py, lpRect) == 0)
    {
        int ii;
        LPBASICWIN pbwin;
        for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
        {
            pbwin = (LPBASICWIN)zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
            if(pbwin->hParent == zapp_pages.APages[pageid].page_index)
            {
                rect.left = pt.px + pbwin->rect.left;
                rect.top  = pt.py + pbwin->rect.top;
                rect.width = pbwin->rect.width;
                rect.height = pbwin->rect.height;
                if(ZIsPointInRect(px, py, &pbwin->rect) == 0)
                {
                    if(pbwin->CallBack != 0&& pbwin->type[0] != (U8)WGUI_IMAGE)
                    {
                    	   MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
		  	   MSG.mtype=MWUI_TOUCH;
		  	   MSG.mparam=type;
		  	   MSG.mparal=px;
		  	   MSG.mparan=py;
                        pbwin->CallBack(&MSG);
                        break;
                    }
                }
            }
        }
        //By Qingfu for page move message and move end message;
        if(ii >= MAX_APP_REG_CTRLS)
        {
            if(type == (U16)KWUI_TOUCH_MOVING || type == (U16)KWUI_TOUCH_MOVE_END)
            {
                if(lpPage->callback)
                {
                    MSG.mitem=pageid;    //erek for C51, 2009-12-20
                    MSG.mtype=MWUI_TOUCH;
                    MSG.mparam=type;
                    MSG.mparal=px;
                    MSG.mparan=py;
                    lpPage->callback(&MSG);
                }
            }
        }
        
        return 0;
    }
    return 255;
}
#endif

extern S16 current_position;

static U8 zmsg_touch_process_by_page(U16 px, U16 py, U16 type, U16 pageid)
{
    LPUIRECT lpRect;
    LPUIPAGE lpPage;
    UIRECT rect;
    ZPoint pt;
    U16 PY_Value;
    //msg_data_t MSG;

	U16 slide_px;

    PY_Value = py & 0x1fff;   //because the hightest three bits contain the touch move directtion, so we must get rid of them.
   //Printf("\npageid=%d\r\n",pageid);

	//eamon.fang 2013.05.07
	/*
	in the slide area(50 <= y <= 50+169)
	x position should plus slide distance
	*/
	if ((PY_Value >= 50) && (PY_Value <= (50+169)))
		slide_px = px + current_position;
	else
		slide_px = px;
	
    if (pageid > MAX_APP_PAGEAS-1)
    {
        return 255;
    }

    lpPage = (LPUIPAGE)zcore_pages_addr[zapp_pages.APages[pageid].page_index];
    lpRect = &lpPage->rect;
    zGetPageStartPoint(&pt, lpPage);
    if (ZIsPointInRect(slide_px, PY_Value, lpRect) == 0)
    {
        int ii;
        LPBASICWIN pbwin;
        for (ii=0; ii<MAX_APP_REG_CTRLS; ii++)
        {
            pbwin = (LPBASICWIN)zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
            if (pbwin->hParent == zapp_pages.APages[pageid].page_index)
            {
                rect.left   = pt.px + pbwin->rect.left;
                rect.top    = pt.py + pbwin->rect.top;
                rect.width  = pbwin->rect.width;
                rect.height = pbwin->rect.height;
                if (ZIsPointInRect(slide_px, PY_Value, &rect) == 0)//erek 2011-5-10, touch deviation
                {
//					if (type == (U16)KWUI_TOUCH_DOWN || type == (U16)KWUI_TOUCH_UP)
					if (type == (U16)KWUI_TOUCH_DOWN || type == (U16)KWUI_TOUCH_UP || type == (U16)KWUI_TOUCH_MOVING || type == (U16)KWUI_TOUCH_MOVE_END)
                	{
						if (pbwin->CallBack != 0 && pbwin->type[0] != (U8)WGUI_IMAGE)
						{
							MSG.mitem  = zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
							MSG.mtype  = MWUI_TOUCH;
							MSG.mparam = type;
							MSG.mparal = slide_px;
//							MSG.mparan = PY_Value;
							MSG.mparan = py;	//eamon.fang
							pbwin->CallBack(&MSG);
							break;
						}
                	}
                }
            }
        }
		//Printf("\nEREKii=%d\r\n",ii);
        //By Qingfu for page move message and move end message;
        //if(ii >= MAX_APP_REG_CTRLS)
        //{
        //Printf("\npageid=%d,type=%d,px=%d,py=%x\r\n",pageid,type,px,py);
            if (type == (U16)KWUI_TOUCH_MOVING || type == (U16)KWUI_TOUCH_MOVE_END)
            {
                if (lpPage->callback)
                {
                    MSG.mitem  = zapp_pages.APages[pageid].page_index;    //Pager ID//erek for C51, 2009-12-20
                    MSG.mtype  = MWUI_TOUCH;
                    MSG.mparam = type;
                    MSG.mparal = px;
                    MSG.mparan = py;
			//Printf("\nMSG.mitem=%d,MSG.mparam=%d,MSG.mparal=%d,MSG.mparan=%d\r\n",MSG.mitem,MSG.mparam,MSG.mparal,MSG.mparan);
                    lpPage->callback(&MSG);
                }
            }
        //}
        
        return 0;
    }
    return 255;
}

U16 ZNeedFocusThisCtrl(U16 ctrl_id)
{
    LPBASICWIN lpbasic;
    int ii;
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == ctrl_id)
            {
                lpbasic = (LPBASICWIN)zcore_ctrls_addr[ctrl_id];
                if(lpbasic->type[0] == WGUI_BUTTON)
                {
                    if(zapp_reg_ctrls.ARegCtrls[ii].value != 0)
                    {
                        return 0;
                    }
                }
                else if(lpbasic->type[0] == WGUI_SCROLLBAR)
                {                
                    if((U8)(zapp_reg_ctrls.ARegCtrls[ii].value&0x80) !=(U8)0)
                    {
                        return 0;
                    }
                }
            }
        }
    }
    return 65535;
}



void ZInvalidateMouseFocus(U16 page_id)
{
    static int ii,Pager_ID;
    //LPBASICWIN lpbasic;
    static U8 value;
    LPUIPBUTTON lpbtn;
    //msg_data_t MSG;

	Pager_ID=page_id;

    if(zapp_pages.APages[zapp_pages.top_index].page_index == Pager_ID)
    {
        for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].pageid == Pager_ID)
            {
                lpbasic = zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
                value = zapp_reg_ctrls.ARegCtrls[ii].value;
                if(lpbasic->type[0] == WGUI_BUTTON)
                {
                    if(value == (U8)0)
                    {
                    	MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
		  	   			MSG.mtype=MWUI_PUBLIC;
		  	   			MSG.mparam=MWUI_CLEAR;
		  	   			MSG.mparal=0;
		  	   			MSG.mparan=0;
                        if(lpbasic->CallBack)lpbasic->CallBack(&MSG);
                        zapp_reg_ctrls.ARegCtrls[ii].value = 1;
                        //lpbasic->OnDraw(lpbasic);    //erek for C51, 2010-02-23
                        lpbtn = (LPUIPBUTTON)lpbasic;
    			   		if((lpbtn->hImageID[0] == (U16)65535) && (lpbtn->textID[0] == (int)65535))
    			   		{
    						//Printf("\nPress1\r\n");
    						;
    			   		}
    			  		 else
    			   		{
    						//Printf("\nlpbtn->hImageID[0]=%x,lpbtn->textID[0] =%x\r\n",lpbtn->hImageID[0],lpbtn->textID[0] );
        					DrawButtonControl((LPUIPBUTTON)lpbasic);
    			   		}
                        //DrawButtonControl((LPUIPBUTTON)lpbasic);
                    }
                }
                else if(lpbasic->type[0] == WGUI_SCROLLBAR)
                {
                    if((value&0x80 )== (U8)0)
                    {   
                    	   MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
		  	   MSG.mtype=MWUI_PUBLIC;
		  	   MSG.mparam=MWUI_CLEAR;
		  	   MSG.mparal=0;
		  	   MSG.mparan=0;
                        if(lpbasic->CallBack)lpbasic->CallBack(&MSG);
                        zapp_reg_ctrls.ARegCtrls[ii].value = value | 0x80;
                       // lpbasic->OnDraw(lpbasic);   //erek for C51, 2010-02-23
                       DrawScrollbarControl((LPUISCROLLBAR)lpbasic);
                    }
                }
            }
        }
    }
}


static U8 zmsg_mouse_process_by_page(U16 px, U16 py, U16 type, U16 pageid)
{
    LPUIRECT lpRect;
    LPUIPAGE lpPage;
    UIRECT rect;
    ZPoint pt;
    //msg_data_t MSG;
	
    if(pageid > MAX_APP_PAGEAS-1)
    {
        return 255;
    }
    lpPage = (LPUIPAGE)zcore_pages_addr[zapp_pages.APages[pageid].page_index];
    lpRect = &lpPage->rect;
    zGetPageStartPoint(&pt, lpPage);
    if(ZIsPointInRect(px, py, lpRect) == 0)
    {
        int ii;
        LPBASICWIN pbwin;
        for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
        {
            pbwin = (LPBASICWIN)zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
            if(pbwin->hParent == zapp_pages.APages[pageid].page_index)
            {
                rect.left = pt.px + pbwin->rect.left;
                rect.top  = pt.py + pbwin->rect.top;
                rect.width = pbwin->rect.width;
                rect.height = pbwin->rect.height;
                if(ZIsPointInRect(px, py, &rect) == 0)
                {
                    if(pbwin->CallBack != 0)
                    {
                    	   MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
		  	   MSG.mtype=MWUI_MOUSE;
		  	   MSG.mparam=type;
		  	   MSG.mparal=px;
		  	   MSG.mparan=py;
                        pbwin->CallBack(&MSG);
                        break;
                    }
                }
            }
        }
        if(ii>=MAX_APP_REG_CTRLS)
        {
            if(lpPage->callback)
            {
            	  MSG.mitem=pageid;    //erek for C51, 2009-12-20
		  MSG.mtype=MWUI_MOUSE;
		  MSG.mparam=type;
		  MSG.mparal=px;
		  MSG.mparan=py;
                lpPage->callback(&MSG);
            }
        }
        
        return 0;
    }
    return 255;
}

#if 0
void ZSetCtrlFocusStatus(U16 ctrl_id)
{
    if(zSetCtrlFocusA(zapp_pages.APages[zapp_pages.top_index].active_crtl_index, 0) == 0)
    {
        zapp_pages.APages[zapp_pages.top_index].active_crtl_index = ctrl_id;
        zSetCtrlFocusA(ctrl_id, 1);
    }
}
#endif


//#define OPEN_CORE_LOG

#ifdef OPEN_CORE_LOG

static void zprintCoreInfo();

#endif

void ZProcessMessage(U8 msg_type, U16 msg_item, U16 param, U16 paral, U16 paran)
{
    static int ii, usr_index, retval;
    LPUIPAGE lpPage;
    LPBASICWIN pbwin;
    //msg_data_t MSG;
	
#ifdef OPEN_CORE_LOG
    if ((U8)0x3a == msg_type && (U16)CMD_IR == param)
    {
        zprintCoreInfo();
        return;
    }
#endif
    
    switch (msg_type)
    {
    case CMD_IR:
    
        for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
            {
                pbwin = zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
                if(pbwin->ir_value == (U16)param)
                {
                     break;
                }
            }
        }

        if(ii<MAX_APP_REG_CTRLS)
        {
            /* Match IR code, callback called */
            if(zapp_reg_ctrls.ARegCtrls[ii].CallBack)
            {
            	  MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
		  MSG.mtype=MWUI_KEYPRESS;
		  MSG.mparam=KEYUI_MENU;
		  MSG.mparal=0;
		  MSG.mparan=0;
                zapp_reg_ctrls.ARegCtrls[ii].CallBack(&MSG);
            }
        }
        else
        {
            if((U16)param == (U16)KWUI_IR_UP || (U16)param == (U16)KWUI_IR_DOWN || (U16)param == (U16)KWUI_IR_LEFT || (U16)param == (U16)KWUI_IR_RIGHT)
            {
                static U8 index, aindex;
                LPUIPAGE ppage;

                for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
                {
                    if((zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == zapp_pages.APages[zapp_pages.top_index].active_crtl_index) && (zapp_reg_ctrls.ARegCtrls[ii].pageid != 255))
                    {
                        break;
                    }
                }
                if(ii>=MAX_APP_REG_CTRLS)
                {
                    //ERROR HERE;
                    return;
                }
                pbwin = zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
                if((U16)param == (U16)KWUI_IR_UP)
                {
                    index =  pbwin->nav_index[NAV_UP];
                    usr_index = KEYUI_UP;
                }
                else if(param == (U16)KWUI_IR_DOWN)
                {
                    index = pbwin->nav_index[NAV_DOWN];
                    usr_index = KEYUI_DN;
                }
                else if(param == (U16)KWUI_IR_LEFT)
                {
                    index = pbwin->nav_index[NAV_LEFT];
                    usr_index = KEYUI_LEFT;
                }
                else
                {
                    index = pbwin->nav_index[NAV_RIGHT];
                    usr_index = KEYUI_RT;
                }
                if(pbwin)
                {
                    if(pbwin->type[0] == (U8)WGUI_SCROLLBAR)
                    {
                    	   MSG.mitem=pbwin->CtrlID;    //erek for C51, 2009-12-20
		  	   MSG.mtype=MWUI_KEYPRESS;
		          MSG.mparam=usr_index;
		  	   MSG.mparal=0;
		  	   MSG.mparan=0;
                        if(pbwin->dExtStyle == (U8)ZSCR_HD && ((U16)param == (U16)KWUI_IR_LEFT || (U16)param == (U16)KWUI_IR_RIGHT))
                        {
                            pbwin->CallBack(&MSG);
                            return;
                        }
                        else if(pbwin->dExtStyle == (U8)ZSCR_VD && ((U16)param == (U16)KWUI_IR_UP || (U16)param == (U16)KWUI_IR_DOWN))
                        {
                            pbwin->CallBack(&MSG);
                            return;
                        }
                    }
                }
                if(index != 255)
                {
                    //navigate when user give nav_direction
                    
                    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
                    {
                        if(zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == (U8)index)
                        {
                            ZSetCtrlFocus(zapp_pages.APages[zapp_pages.top_index].active_crtl_index, 0);
                            zapp_pages.APages[zapp_pages.top_index].active_crtl_index = zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;
                            ZSetCtrlFocus(zapp_pages.APages[zapp_pages.top_index].active_crtl_index, 1);
                            zapp_pages.APages[zapp_pages.top_index].reg_ctrl_index = ii;
                            return ;
                        }
                    }
                }
                else
                {
                    lpPage = zcore_pages_addr[zapp_pages.APages[zapp_pages.top_index].page_index];
                    if(lpPage->callback)
                    {
                    	   MSG.mitem=zapp_pages.APages[zapp_pages.top_index].page_index;    //erek for C51, 2009-12-20
		  	   MSG.mtype=MWUI_KEYPRESS;
		          MSG.mparam=usr_index;
		  	   MSG.mparal=0;
		  	   MSG.mparan=0;
                        lpPage->callback(&MSG);
                    }
                }
            }
            else if((U16)param == (U16)KWUI_IR_MENU || (U16)param == (U16)KWUI_IR_SEL)
            {
                //enter or menu key press
                WORD index;
                index = zapp_pages.APages[zapp_pages.top_index].active_crtl_index;
                for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
                {
                    if(index == zapp_reg_ctrls.ARegCtrls[ii].ctrl_id)
                    {
                        break;
                    }
                }
                if(ii<MAX_APP_REG_CTRLS)
                {
                    if(zapp_reg_ctrls.ARegCtrls[ii].CallBack)
                    {
                    	    MSG.mitem=index;    //erek for C51, 2009-12-20
		  	    MSG.mtype=MWUI_KEYPRESS;
		           MSG.mparam=KEYUI_MENU;
		  	    MSG.mparal=0;
		  	    MSG.mparan=zapp_reg_ctrls.ARegCtrls[ii].value;
                        zapp_reg_ctrls.ARegCtrls[ii].CallBack(&MSG);
                    }
                }
                else
                {
                    if(zapp_pages.APages[zapp_pages.top_index].page_index>=zcore_pages_number)
                    {
                        return;
                    }
                    lpPage = zcore_pages_addr[zapp_pages.APages[zapp_pages.top_index].page_index];
                    if(lpPage)
                    {
                        if(lpPage->callback == 0)
                        {
                             zmsg_process_by_desktop(msg_type, param, paral, 0);
                             break;
                        }
                        if(param == (U16)KWUI_IR_EXIT)
                            param = KEYUI_EXIT;

			    MSG.mitem=lpPage->PageID;    //erek for C51, 2009-12-20
		  	    MSG.mtype=MWUI_KEYPRESS;
		           MSG.mparam=param;
		  	    MSG.mparal=paral;
		  	    MSG.mparan=0;
                        if(lpPage->callback(&MSG) == -1)
                        {
                              zmsg_process_by_desktop(msg_type, param, paral, 0);
                        }
                    }
                }
            }
            else 
            {
                //message will be process by top windows if top windows return -1
                if(zapp_pages.APages[zapp_pages.top_index].page_index>=zcore_pages_number)
                {
                    return;
                }
                lpPage = zcore_pages_addr[zapp_pages.APages[zapp_pages.top_index].page_index];
                if(lpPage->callback == 0)
                {
                     zmsg_process_by_desktop(msg_type, param, paral, 0);
                     break;
                }
                if(param == (U16)KWUI_IR_EXIT)param = KEYUI_EXIT;

		  MSG.mitem=lpPage->PageID;    //erek for C51, 2009-12-20
		  MSG.mtype=MWUI_KEYPRESS;
		  MSG.mparam=param;
		  MSG.mparal=paral;
		  MSG.mparan=0;
                if(lpPage->callback(&MSG) == -1)
                {
                     zmsg_process_by_desktop(msg_type, param, paral, 0);
                }
            }
        }
    break;
    /*
        key board message process: will map to IR memsage process;
    */
    case CMD_KEY:
    {
        int ir_key = -1;
        switch(param)
        {
        	case KWUI_KEYPAD_UP:
            		ir_key = KWUI_IR_UP;
        	break;
        	case KWUI_KEYPAD_DOWN:
            		ir_key = KWUI_IR_DOWN;
        	break;
        	case KWUI_KEYPAD_LEFT:
            		ir_key = KWUI_IR_LEFT;
        	break;
        	case KWUI_KEYPAD_RIGHT:
            		ir_key = KWUI_IR_RIGHT;
        	break;
        	case KWUI_KEYPAD_MENU:
            		ir_key = KWUI_IR_MENU;
        	break;
		case KWUI_KEYPAD_SEL:
            		ir_key = KWUI_IR_SEL;
        	break;
        	case KWUI_KEYPAD_EXIT:
            		ir_key = KWUI_IR_EXIT;
        	break;
        }
        if(ir_key != -1)
        {
            ZProcessMessage(CMD_IR, msg_item, ir_key, paral, paran);
        }
    }
    break;
    case CMD_TOUCH:
        if (zmsg_touch_process_by_page(paral, paran, param, zapp_pages.top_index) == (U8)255)
        {
            for (ii=1; ii<MAX_APP_PAGEAS; ii++)
            {
                if (PAGE_ACTIVE == zapp_pages.APages[ii].status)
                {
                    if (zmsg_touch_process_by_page(paral,paran,param,ii) != (U8)255)
                    {
                        break;
                    }
                }
            }
            zmsg_touch_process_by_page(paral,paran,param,0);
        }
    break;
    case CMD_MOUSE:
        if(zmsg_mouse_process_by_page(paral, paran, param, zapp_pages.top_index) == (U8)255)
        {
            for(ii=1;ii<MAX_APP_PAGEAS;ii++)
            {
                if(PAGE_ACTIVE == zapp_pages.APages[ii].status)
                {
                    if(zmsg_mouse_process_by_page(paral, paran, param, ii) != (U8)255)
                    {
                        break;
                    }
                }
            }
            if(ii>=MAX_APP_PAGEAS)
            {
                zmsg_mouse_process_by_page(paral, paran, param, 0);
            }
        }
    break;
    case CMD_TIMER:            
        for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == (U8)param)
            {
                MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
		  MSG.mtype=MWUI_PUBLIC;
		  MSG.mparam=MWUI_TIMER;
		  MSG.mparal=paral;
		  MSG.mparan=0;
                 zapp_reg_ctrls.ARegCtrls[ii].CallBack(&MSG);
                 break;
            }
        }
    break;
    
    case CMD_USER:
        switch(param)
        {
        case MWUI_CREATE_PAGE:
            if(msg_item<(U16)zcore_pages_number)
            {
                ZCreatePage(msg_item);
                return;
            }
        break;
        case MWUI_UPDATE:
        break;
        }
        for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
        {
            if(zapp_reg_ctrls.ARegCtrls[ii].ctrl_id != 65535)
            {
                pbwin = zcore_ctrls_addr[zapp_reg_ctrls.ARegCtrls[ii].ctrl_id];
                if(pbwin->CtrlID == (U16)msg_item)
                {
                	MSG.mitem=zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;    //erek for C51, 2009-12-20
		  	MSG.mtype=MWUI_TOUCH;//MWUI_PUBLIC;//2010-12-6 erek for realizing the button three status
		  	MSG.mparam=param;
		  	MSG.mparal=paral;
		  	MSG.mparan=paran;
                     zapp_reg_ctrls.ARegCtrls[ii].CallBack(&MSG);
                     break;
                }
            }
        }
       
    break;
    case CMD_SYSTEM:
    break;
    }
}


U8 ZGetCtrlParam(U16 ctrl_id, U8 type, U8 *pval)
{
    int ii;U8 * ptr;
    U8 retval=ZRET_ERROR;
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == ctrl_id)
        {
            break;
        }
    }
    if(ii>=MAX_APP_REG_CTRLS)
    {
        return ZRET_ERROR;
    }
    *pval = zapp_reg_ctrls.ARegCtrls[ii].value;
    return ZRET_OKEY;    
}

U8 ZSetCtrlParam(U16 ctrl_id, U8 type, U8 value)
{
    int ii;
    U8 retval=ZRET_ERROR, *ptr, val;

    for (ii=0; ii<MAX_APP_REG_CTRLS; ii++)
    {
        if (zapp_reg_ctrls.ARegCtrls[ii].ctrl_id == ctrl_id)
        {
            break;
        }
    }

    if (ii >= MAX_APP_REG_CTRLS)
    {
        return ZRET_ERROR;
    }

    zapp_reg_ctrls.ARegCtrls[ii].value = value;

    return ZRET_OKEY;
}

static void zinit_sys_queue(msg_que_t* pmq)
{
    memset(pmq, 0, sizeof(msg_que_t));
}

U16 ZGetMessage(msg_data_t * msg)
{
    zmsg_lock();
    if(zsys_que.msg[zsys_que.out].flag != (U16)1)
    {
        zmsg_unlock();
        return 255;
    }
    *msg = zsys_que.msg[zsys_que.out];
    zsys_que.msg[zsys_que.out].flag = (U16)0;
        
    zsys_que.out ++;
    if(zsys_que.out >= QUE_MSG_NUMBER)
    {
          zsys_que.out = 0;
    }    
    zmsg_unlock();
    return 0;
}

static U16 zput_sys_que_message(msg_data_t * msg)
{
    
    zmsg_lock();
    zsys_que.msg[zsys_que.in] = *msg;
    zsys_que.msg[zsys_que.in].flag = (U16)1;
    zsys_que.in ++;
    if(zsys_que.in >= QUE_MSG_NUMBER)
    {
          zsys_que.in = 0;
    }  
    zmsg_unlock();
    
    return 0;
}

static void zusleep(int usec)
{
    ;
}

static void zmsg_lock_init()
{
    zsys_lock.l2 = '0';
    zsys_lock.l1 = '0';
}

static void zmsg_lock()
{
    static U8 flag = 0;
    
    return ;

    flag ++;
    
    if('0' == zsys_lock.l1 )
    {
        zsys_lock.l1 = '1';
        if('0' == zsys_lock.l2)
        {
            zsys_lock.l2 = '1';
        }
        else
        {
            while('1' == zsys_lock.l1)
            { 
                zusleep(5000);    
            }
        }
        
    }
    else
    {
        while('1' == zsys_lock.l1)
        {
            zusleep(5000);
        }
    }
}
static void zmsg_unlock()
{
    zsys_lock.l2 = '0';
    zsys_lock.l1 = '0';
}

void ZRegisterCtrls(void**addr,U16 number)

{
    zcore_ctrls_addr = addr;
    zcore_ctrls_number = number;
	//Printf("\nnumber:%x\r\n",number);
}

void ZRegisterPages(void**addr,U16 number)

{
    zcore_pages_addr = addr;
    zcore_pages_number = number;
}


U16 zIsActivePage(U8 page_id)
{
    int ii;
    
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status == (U8)PAGE_ACTIVE && page_id == zapp_pages.APages[ii].page_index)
        {
            return 0;
        }
    }
    return 65535;
}


U16 zIsActiveCtrl(U16 ctrlid)
{
    int ii;
    
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255 && ctrlid == zapp_reg_ctrls.ARegCtrls[ii].ctrl_id)
        {
            return 0;
        }
    }
    return 65535;
}



#ifdef OPEN_CORE_LOG

static void zprintCoreInfo()
{
    int ii,jj,num,lii,ljj,cnumber;
    char pindex[MAX_APP_REG_CTRLS],sbuf[64];
    return ;
    
    cnumber = 0;
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
        {
            cnumber ++;
        }
    }
    ZPrintf_XY(200, 50, "core ctrls=%d, app ctrls=%d, active strls=%d", MAX_APP_REG_CTRLS, zcore_ctrls_number,cnumber);
    num = 0;
    for(ii=0;ii<MAX_APP_PAGEAS;ii++)
    {
        if(zapp_pages.APages[ii].status == PAGE_ACTIVE)
        {
            pindex[num] = ii;
            num ++;
        }
    }
    ZPrintf_XY(200, 80,20,50, "core pages=%d, app pages=%d, active pages=%d", MAX_APP_PAGEAS, zcore_pages_number,num);
    ZPrintf_XY(200, 110, 20,50,"top page = %d; active page id:", zapp_pages.top_index);    
    
    for(ii=0;ii<num;ii++)
    {
        ZPrintf_XY(420+ii*6, 110, 20,50,"[%d]",pindex[ii]);    ;
    }
    cnumber = 0;
    for(ii=0;ii<MAX_APP_REG_CTRLS;ii++)
    {
        if(zapp_reg_ctrls.ARegCtrls[ii].pageid != (U8)255)
        {
            pindex[cnumber] = zapp_reg_ctrls.ARegCtrls[ii].ctrl_id;
            cnumber ++;
        }
    }
    lii = (cnumber+7) / 8;
    ZPrintf_XY(200, 140, 20,50,"active ctrl (%d):", cnumber);
    
    for(ii=0;ii<lii;ii++)
    {
        ZPrintf_XY(200, 170+ii*30, 20,50,"%s", sbuf);
    }
    
    
}


#endif






